<?php
/**
* 统计玩家留存 计划任务每天00:05分跑
*
* @copyright GZFeiYin 凡人项目开发团队
* @author phper wanghan
* @package
* @subpackage
* @version 2012-11-19
* php task_stat_player_left.php
*/

	//每个计划任务都必须包含
	require("task_header.php");
	
	$yesterday = date('Y-m-d',strtotime('-1 days'));
	$sSql = "select o.player_id,max(o.dDate) as eday,date_format(p.create_dt,'%Y-%m-%d') as sday 
		from g_player_online o, ".DB_FANREN2.".t_player p 
		where o.player_id = p.player_id
		group by o.player_id 
		having eday > sday  AND eday>='{$yesterday}'";

		
		$aData = array();
		$total = 0;
		$aRs = $db2->query_to_array($sSql);
		//统计的天数
		$stat_days = array(1,2,3,4,5,6,7,14,30);
		foreach($aRs as $row)
		{
			$continday = (strtotime($row['eday'])-strtotime($row['sday']))/86400;
			if(in_array($continday, $stat_days)){
				$aData[$row['sday']][$continday]['num']++;
			}
		}

		foreach ($aData as $kdate=>$v1){
			foreach ($v1 as $k=>$v){
				//先删除再插入
				$db2->query("DELETE FROM ".DB_FANREN2_ADMIN.".g_player_left WHERE dDate='{$kdate}' AND left_day={$k}");
				
				$sSql = "INSERT INTO 
								".DB_FANREN2_ADMIN.".g_player_left 
							SET 
								dDate='".$kdate."', 
								left_day=".$k.",
								player_num=".$v['num'];
				
				$db2->query($sSql);
			}			
		}